package com.vivebest.utils.kd.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

import javax.annotation.PostConstruct;

/**
 * <p>
 *     redis配置
 * </p>
 *
 * @author zsk
 * @date 2020/8/15 下午4:48
 */
@Configuration
public class RedisConfig {

    private final RedisTemplate<Object,Object> redisTemplate;

    @Autowired
    public RedisConfig(RedisTemplate<Object,Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }


    /**
     * 设置RedisTemplate的序列化器
     */
    private void initRedisTemplate() {
        RedisSerializer<String> stringSerializer = redisTemplate.getStringSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
    }


    /**
     * 自定义初始化方法
     */
    @PostConstruct
    public void init() {
        initRedisTemplate();
    }
}
